﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegatesAndEvents
{
    class PersonV2 : IPropertyChangeNotification
    {
        //public event PropertyChangedEventHandler propertyChangedEventHandler;
        private string firstName;
        private string lastName;
        private string gender;
        private string birthDate;

        public PersonV2(string firstName, string lastName, string gender, string birthDate)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.gender = gender;
            this.birthDate = birthDate;
            propertyChangedEventHandler += delegate(object sender, PropertyChangedEventArgs args)
            {
                Console.WriteLine("property {0} was changed, old value: {1}   new value: {2}",args.property, args.oldValue, args.newValue);
            };
        }

        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                OnPrpertyChanged("firstName", this.firstName, value);
                firstName = value;
            }
        }

        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                OnPrpertyChanged("lastName", this.lastName, value);
                lastName = value;
            }
        }

        public string Gender
        {
            get
            {
                return gender;
            }
            set
            {
                OnPrpertyChanged("gender", this.gender, value);
                gender = value;
            }
        }

        public string BirthDate
        {
            get
            {
                return birthDate;
            }
            set
            {
                OnPrpertyChanged("birthDate", this.birthDate, value);
                birthDate = value;
            }
        }

        protected void OnPrpertyChanged(string property, object oldValue, object newValue) 
        {
            if (propertyChangedEventHandler != null) 
            {
                PropertyChangedEventArgs args = new PropertyChangedEventArgs(property, oldValue, newValue);
                propertyChangedEventHandler(this, args);
            }
        }


        public event PropertyChangedEventHandler propertyChangedEventHandler;
    }
    public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs eventArgs);
}
